const app = getApp()
const util = require('../../utils/util.js');
const api = require('../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      //套餐组合商品
      packageDatas: {},
      //页面颜色
      mainColor: app.pageColor.mainColor,
  },
    
  packageSuccessCallBack: function (res) {

      for (var i = 0; i < res.composers.length; i++) {
          for (var j = 0; j < res.composers[i].goodDetail.length; j++) {
              res.composers[i].goodDetail[j].name = res.composers[i].goodDetail[j].name.length >= 9 ? res.composers[i].goodDetail[j].name.substring(0, 9) + "..." : res.composers[i].goodDetail[j].name;
          }
      }

      for (var i = 0; i < res.composers.length; i++) {
          var oldPrice = 0;
          for (var j = 0; j < res.composers[i].goodDetail.length; j++) {
              res.composers[i].goodDetail[j].name = res.composers[i].goodDetail[j].name.length >= 9 ? res.composers[i].goodDetail[j].name.substring(0, 9) + "..." : res.composers[i].goodDetail[j].name;
              oldPrice += parseInt(res.composers[i].goodDetail[j].price)
          }
          res.composers[i].oldPrice = oldPrice;
      }
      res.title = "套餐优惠专区"
      this.setData({
          packageDatas: res,
      })
      
  },

  /**
   * 是否库存充足
   */
  hasStock:function (stock) {
    if(stock<=0){
        util.failToast("库存不足");
        return false;
    }
    return true;
  },

  /**
   * 获取商品信息
   */
  getGoodInfo:function(position){
    var packageGood = this.data.packageDatas.composers[position];
    //存入对象
    var selectGoodInfo={};
    selectGoodInfo.goodId = packageGood.id;
    selectGoodInfo.goodName=packageGood.name.length>25?packageGood.name.substring(0,25)+"...":packageGood.name;
    selectGoodInfo.goodPrice = packageGood.price;
    selectGoodInfo.goodOldPrice = packageGood.price;
    selectGoodInfo.goodDes = "";
    selectGoodInfo.goodStock = packageGood.stock;
    selectGoodInfo.goodCover = packageGood.cover;
    selectGoodInfo.goodCount = 1;
    selectGoodInfo.skuName = "";
    selectGoodInfo.goodSales = packageGood.sales;
    selectGoodInfo.skuId = "";
    selectGoodInfo.state = 0;
    return selectGoodInfo;
  },

  /**
   * 添加购物车
   */
  addShop:function (options) {
    var position = options.currentTarget.dataset.position;
    var packageGood = this.data.packageDatas.composers[position];
    if(this.hasStock(packageGood.stock)){
        var goodInfo = this.getGoodInfo(position);
        var selectGoodInfoArr = util.getStorage("selectGoodInfoArr")==""?[]:util.getStorage("selectGoodInfoArr")
        var count =1;
        for (var i = 0; i < selectGoodInfoArr.length;i++){
            if (goodInfo.goodId == selectGoodInfoArr[i].goodId){
                selectGoodInfoArr[i].goodCount +=1 ;
                count = 2;
                break;
            }
        }
        if (count ==1){
            selectGoodInfoArr.push(goodInfo);
        }
        util.setStorage("selectGoodInfoArr", selectGoodInfoArr);
        util.successToast("加入购物车成功") 
    }

    
  },


  //立即购买
  buy: function (options) {
   
    var packageGood = this.data.packageDatas.composers[position];
    var goodInfoArr=[]
    if(this.hasStock(packageGood.stock)){
        var goodInfo = this.getGoodInfo(position);
        goodInfoArr.push(goodInfo)
        this.navigateTo("../firmOrder/firmOrder?goodInfoArr=" + JSON.stringify(goodInfoArr))
    
    }
    
},

    /**
     * 页面跳转
     */

    navigateTo: function (url) {
        wx.navigateTo({
            url: url
        })
    },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      //获取套餐商品
      let url = api.Composer();
      util.http(true, "GET", url, {}, this.packageSuccessCallBack);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})